// pages/address/address.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name:"",
        phone_number:"",
        address:"",
        id:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var id = data.id;
                that.init(id);
                that.setData({
                    id:id
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    init: function (id) {
        var that=this;
        var params = {};
        params['id'] = id;
        wx.request({
            url: app.config() + "member/memberAddress",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var data=res.data.data;
                that.setData({
                    name:data.name,
                    phone_number:data.phone_number,
                    address: data.address
                })
            }
        })
    },
    name_input:function(e){
        var that=this;
        var name=e.detail.value;
        that.setData({
            name:name
        })
    },
    phone_number_input:function(e){
        var that=this;
        var phone_number=e.detail.value;
        that.setData({
            phone_number: phone_number
        })
    },
    address_input:function(e){
        var that=this;
        var address=e.detail.value;
        that.setData({
            address: address
        })
    },
    submit:function(){
        var that=this;
        var name = that.data.name;
        var phone_number = that.data.phone_number;
        var address = that.data.address;
        var id=that.data.id;
        var params={};
            params['name']=name;
            params['phone_number']=phone_number;
            params['address']=address;
            params['id']=id;
        wx.request({
            url: app.config() + "member/updateMemberAddress",
            data:params,
            header:{
                'content-type':'application/json'
            },
            success:function(res){
                if(res.data.status==0){
                    wx.showToast({
                        title: '更新成功',
                        icon:"success"
                    })
                    wx.switchTab({
                        url: '../../pages/myPage/myPage'
                    })
                }
            }
        })
    }
})